#!/usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import cv2
from PIL import Image

def pic_test1():
	# 加载图片
	img = cv2.imread('C:\\Users\\ruido\\Desktop\\1.bmp')
	cv2.imshow('1', img)

	# 模糊降噪
	img1 = cv2.blur(img, (5, 5)) #(5，5)数值越大，越模糊；前者数值>后者数值为水平方向模糊，否则为垂直模糊
	cv2.imshow('2', img1)

	# 转化至灰度照片
	img2 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
	cv2.imshow('3', img2)

	# canny边缘检测
	canny = cv2.Canny(img, 20, 40)
	cv2.imshow('4', canny)

	# 霍夫变换圆检测
	# img2必须为灰度图像
	# 40：圆心与圆心间的最短距离，太小检测出很多圆，太大又会漏掉一些圆的检测
	# 30：检测的圆的最小半径
	# 50：检测的圆的最大半径
	circles = cv2.HoughCircles(img2, cv2.HOUGH_GRADIENT, 1, 40, param1=40, param2=20, minRadius=30, maxRadius=50)
	#[[[221.5 195.5  48.1]
	#  [602.5 340.5  47. ]
	#  [219.5 486.5  47.4]
	#  [153.5 340.5  46.7]
	#  [528.5 187.5  47.2]
	#  [371.5 125.5  46.7]
	#  [378.5 547.5  47. ]
	#  [532.5 502.5  47.1]]]
	print(circles) # [221.5 195.5  48.1] 分别是圆心坐标（x,y）和半径
	print(len(circles[0])) # 检测出的圆的个数 8个

	# 把检测出的圆进行标注
	for circle in circles[0]:
		# 圆心坐标
		x = int(circle[0])
		y = int(circle[1])
		# 圆的半径
		r = int(circle[2])
		# （255，0，0）：蓝色，用指定颜色标注
		# 1：正数为空心圆，复数为实心圆
		# 8：线条类型（0，4，8）
		# 0：网上的解释“圆心坐标和半径的小数点位数”并不懂，实际操作的时候，发现不能为负，值越大，画的圆越小，而且所画圆越偏离至左上角，得请大神指导了
		img = cv2.circle(img, (x, y), r, (255, 0, 0), 1, 8, 0) # 画圆
		img = cv2.circle(img, (x, y), 1, (255, 0, 0), 1, 8, 0) # 圆心其实就是一个半径很小的圆
	cv2.imshow('5', img)

	# 任意键退出
	cv2.waitKey(0)
	cv2.destroyAllWindows()



def pic_test2():

	#pmj   2021-3-29  绘制圆形

	img=np.ones((1600,2560,3),dtype="uint8")*255
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))
	yanse=[[255,0,0],[0,255,0],[0,0,255],[0,0,0]]
	for i in range(0, 1000, 10):
		cv2.circle(img,(X,Y),i,yanse[i//10%4],i%7)
	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test2_1600x2560.jpg", img)


	img=np.ones((1280,2048,3),dtype="uint8")*255
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))
	yanse=[[255,0,0],[0,255,0],[0,0,255],[0,0,0]]
	for i in range(0, 1000, 10):
		cv2.circle(img,(X,Y),i,yanse[i//10%4],i%7)
	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test2_1280x2048.jpg", img)

	img=np.ones((1200,1920,3),dtype="uint8")*255
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))
	yanse=[[255,0,0],[0,255,0],[0,0,255],[0,0,0]]
	for i in range(0, 1000, 10):
		cv2.circle(img,(X,Y),i,yanse[i//10%4],i%7)
	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test2_1200x1920.jpg", img)



def pic_test3():

	#pmj   2021-3-29  绘制圆形

	img=np.ones((1280,2048,3),dtype="uint8")*255
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))
	yanse=[[255,0,0],[0,255,0],[0,0,255],[0,0,0]]

	for i in range(0, 1280, 50):
		cv2.line(img, (i, 0), (2048, 1280-i), yanse[i//10%4], 10, 4)
		#cv2.line(img, (0, i), (2048, 1280-i), yanse[1], 1, 4)

	for i in range(0, 2048, 50):
		cv2.line(img, (0, i), (2048-i, 1280), yanse[i//10%4], 10, 4)
		#cv2.line(img, (0, i), (2048, 1280-i), yanse[1], 1, 4)

	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test3.jpg", img)



def pic_test4():

	#pmj   2021-3-29  绘制圆形

	img=np.ones((1280,2048,3),dtype="uint8")*255
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))
	yanse=[[255,0,0],[0,255,0],[0,0,255],[0,0,0]]

	#for i in range(0, 1280, 10):
	#cv2.line(img, (10, 10), (10, 1280), yanse[1], 10, 4)  #00 05  10 15 20 25
	#cv2.line(img, (20, 10), (20, 1280), yanse[3], 10, 4)
	for i in range(0, 2048, 50):
		cv2.line(img, (i, 0), (i, 1280), yanse[i//10%4], 20, 4)


	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test4.jpg", img)

def pic_test5():

	img=np.ones((1280,2048,3),dtype="uint8")*255
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))
	yanse=[[255,0,0],[0,255,0],[0,0,255],[0,0,0]]

	#for i in range(0, 1280, 10):
	#cv2.line(img, (10, 10), (10, 1280), yanse[1], 10, 4)  #00 05  10 15 20 25
	#cv2.line(img, (20, 10), (20, 1280), yanse[3], 10, 4)
	for i in range(0, 1280, 50):
		cv2.line(img, (0, i), (2048, i), yanse[i//10%4], 20, 4)


	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test5.jpg", img)


def pic_test6():

	img=np.ones((1280,2048,3),dtype="uint8")*0
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))
	yanse=[[255,0,0],[0,255,0],[0,0,255],[255,255,255]]
	yanse_s=[[255,0,0],[0,255,0],[0,0,255]]

	#for i in range(0, 1280, 10):
	#cv2.line(img, (10, 10), (10, 1280), yanse[1], 10, 4)  #00 05  10 15 20 25
	#cv2.line(img, (20, 10), (20, 1280), yanse[3], 10, 4)
	y=0
	for i in range(0, 2048, 200):
		y=y+1
		#cv2.line(img, (0, i), (2048, i), yanse[y%4], 100, 4)
		cv2.line(img, (i, 0), (i, 1280), yanse_s[i%3], 100, 4)
		cv2.line(img, (0, i), (2048, i), yanse_s[i%3], 100, 4)



	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test6.jpg", img)


def pic_test7():

	img=np.ones((1280,2048,3),dtype="uint8")*255
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))

	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test7.jpg", img)

def pic_test8():

	img=np.ones((1280,2048,3),dtype="uint8")*0
	(X,Y)=(round(img.shape[1]/2),round(img.shape[0]/2))

	cv2.namedWindow('yanshi')
	cv2.imshow('yanshi',img)
	cv2.waitKey(0)
	cv2.destroyAllWindows()
	cv2.imwrite("test8.jpg", img)

'''
红色 【RGB】 255, 0, 0 
橙色 【RGB】 255, 165, 0  
黄色 【RGB】 255, 255, 0 
绿色 【RGB】 0, 255, 0 
青色 【RGB】 0, 255, 255  
蓝色 【RGB】 0, 0, 255 
紫色 【RGB】 139, 0, 255 
'''
def pic_test9():
	# 创建一张图1920*1080分辨率
	img=Image.new("RGB",(2048,1280),(255,0,0))
	# 保存图片
	img.save("红色.jpg")

	img=Image.new("RGB",(2048,1280),(0,255,0))
	img.save("绿色.jpg")
	img=Image.new("RGB",(2048,1280),(0,0,255))
	img.save("蓝色.jpg")
	img=Image.new("RGB",(2048,1280),(0,255,255))
	img.save("青色.jpg")
	img=Image.new("RGB",(2048,1280),(255,255,0))
	img.save("黄色.jpg")

	img=Image.new("RGB",(2048,1280),(255,165,0))
	img.save("橙色.jpg")

	img=Image.new("RGB",(2048,1280),(139,0,255))
	img.save("紫色.jpg")




#import imageio
#def compose_gif():
#    img_paths = ["test2.jpg","test3.jpg","test4.jpg","test5.jpg","test6.jpg","test7.jpg","test8.jpg"
#		,"橙色.jpg","红色.jpg","黄色.jpg","蓝色.jpg","绿色.jpg","青色.jpg","紫色.jpg"]
#    gif_images = []
#    for path in img_paths:
#        gif_images.append(imageio.imread(path))
#    imageio.mimsave("test.gif",gif_images,fps=1)




import matplotlib.pyplot as plt
import matplotlib.animation as animation

def huayuan_gif():
	fig, ax = plt.subplots()
	#fig.patch.set_alpha(0.)  # 设置透明背景，但animation不起作用
	plt.axis('off')          # 关闭坐标轴
	#初始化窗口和坐标轴
	xdata, ydata = [], []
	#初始化x,y列表
	ln, = ax.plot([], [], 'r-', animated=False)
	#初始化绘制曲线的参数。
	#init()函数初始化x,y轴范围
	def init():
	    ax.set_xlim(-20,20)
	    ax.set_ylim(-20,15)
	    return ln,
	#迭代器，根据传入的frame更新x,y值
	def update(frame):
	    # 心形函数
	    x = 16 * np.sin(frame) ** 3
	    y = 13 * np.cos(frame) - 5 * np.cos(2 * frame) - 2 * np.cos(3 * frame) - np.cos(4 * frame)
	    xdata.append(x)
	    ydata.append(y)
	    ln.set_data(xdata, ydata)
	    return ln,
	# interval：帧持续时间(milliseconds)
	anim = animation.FuncAnimation(fig, update, frames=np.linspace(0, 10, 100), init_func=init, interval=1, repeat=False, blit=True)
	anim.save('tmp.gif', writer='pillow')
	plt.show()

def huaxin_gif():
	fig, ax = plt.subplots()
	# fig.patch.set_alpha(0.)  # 设置透明背景，但animation不起作用
	plt.axis('off')          # 关闭坐标轴
	#初始化窗口和坐标轴
	xdata, ydata = [], []
	#初始化x,y列表
	ln, = ax.plot([], [], 'r-', animated=False)
	#初始化绘制曲线的参数。
	#init()函数初始化x,y轴范围

	theta = np.arange(0, 2*np.pi, 0.01)
	r=2.0
	a,b=(0.0)

	def init():
	    ax.set_xlim(-20,20)
	    ax.set_ylim(-20,15)
	    return ln,
	#迭代器，根据传入的frame更新x,y值
	def update(frame):
	    # 心形函数
	    x = a + r * np.cos(theta)
	    y = b + r * np.sin(theta)
	    xdata.append(x)
	    ydata.append(y)
	    ln.set_data(xdata, ydata)
	    return ln,
	# interval：帧持续时间(milliseconds)
	anim = animation.FuncAnimation(fig, update, frames=np.linspace(0, 10, 100), init_func=init, interval=1, repeat=False, blit=True)
	anim.save('tmp.gif', writer='pillow')
	plt.show()

pic_test2()
pic_test3()
pic_test4()
pic_test5()
pic_test6()
pic_test7()
pic_test8()
pic_test9()
#compose_gif()
#huaxin_gif()

huayuan_gif()

